﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 蓝牙注册工具.View
{
    /// <summary>
    /// DataView.xaml 的交互逻辑
    /// </summary>
    public partial class DataView : UserControl
    {
        private BtLabelDAL.MacDAL macDAL = new BtLabelDAL.MacDAL();
        private ViewModel.DataViewModel viewModel = new ViewModel.DataViewModel();
        public DataView()
        {
            //设置为中文
            HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-cn");
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void datePicker1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (datePicker1.Text == "" || datePicker2.Text == "")
                {
                    return;
                }
                TextBoxMac.Text = "";
                DateTime dateTime1 = DateTime.Parse(datePicker1.Text);
                DateTime dateTime2 = DateTime.Parse(datePicker2.Text).AddDays(1);
                List<BtLabelModel.MacDbModel.macs> models = new List<BtLabelModel.MacDbModel.macs>();
                if (dateTime1 > dateTime2)
                {
                    initList(models);
                    return;
                }
                models = macDAL.GetModelList(dateTime1, dateTime2).OrderBy(it => it.time).ToList();
                initList(models);
            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void datePicker2_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (datePicker1.Text == "" || datePicker2.Text == "")
                {
                    return;
                }
                TextBoxMac.Text = "";
                DateTime dateTime1 = DateTime.Parse(datePicker1.Text);
                DateTime dateTime2 = DateTime.Parse(datePicker2.Text).AddDays(1);
                List<BtLabelModel.MacDbModel.macs> models = new List<BtLabelModel.MacDbModel.macs>();
                if (dateTime1 > dateTime2)
                {
                    initList(models);
                    return;
                }
                models = macDAL.GetModelList(dateTime1, dateTime2).OrderBy(it => it.time).ToList();
                initList(models);

            }
            catch (Exception ex)
            {
                Growl.Error(ex.Message);
                BtLabelTool.LogHelper.WriteLog(ex.ToString());
            }
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                datePicker1.Text = "";
                datePicker2.Text = "";
                string mac = TextBoxMac.Text.Trim().ToUpper();
                mac = mac.Substring(0, 2) + " " + mac.Substring(2, 2) + " " +
                    mac.Substring(4, 2) + " " + mac.Substring(6, 2) + " " +
                    mac.Substring(8, 2) + " " + mac.Substring(10, 2);
                BtLabelModel.MacDbModel.macs model = macDAL.GetModel(mac);
                List<BtLabelModel.MacDbModel.macs> models = new List<BtLabelModel.MacDbModel.macs>();
                if (model == null)
                {
                    initList(models);
                    return;
                }
                models.Add(model);
                initList(models);
            }
            else
            {
                if (datePicker1.Text == "" || datePicker2.Text == "")
                {
                    return;
                }
                DateTime dateTime1 = DateTime.Parse(datePicker1.Text);
                DateTime dateTime2 = DateTime.Parse(datePicker2.Text).AddDays(1);
                List<BtLabelModel.MacDbModel.macs> models = new List<BtLabelModel.MacDbModel.macs>();
                if (dateTime1 > dateTime2)
                {
                    initList(models);
                    return;
                }
                models = macDAL.GetModelList(dateTime1, dateTime2, TextBoxMac.Text).OrderBy(it => it.time).ToList();
                initList(models);
            }

        }
        private void initList(List<BtLabelModel.MacDbModel.macs> models)
        {
            List<ViewModel.DataViewModel.MacItem> macItems = new List<ViewModel.DataViewModel.MacItem>() { };
            for (int i = 0; i < models.Count; i++)
            {
                ViewModel.DataViewModel.MacItem macItem = new ViewModel.DataViewModel.MacItem()
                {
                    No = (i + 1).ToString(),
                    Mac = models[i].mac,
                    TestTime = models[i].time.ToString("yyyy-MM-dd HH:mm:sss"),
                    BackColor = BtLabelModel.ColorModel.FirstColor
                };
                //if (models[i].image != null && models[i].image != "")
                //{
                //    var base64 = models[i].image;
                //    byte[] bytes = Convert.FromBase64String(models[i].image);
                //    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
                //    {
                //        BitmapImage bitmapImage = new BitmapImage();
                //        bitmapImage.BeginInit();
                //        bitmapImage.StreamSource = ms;
                //        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                //        bitmapImage.EndInit();
                //        macItem.Pic = bitmapImage;
                //        macItem.PicVisibility = Visibility.Visible;
                //    }
                //}

                if (i % 2 != 0)
                {
                    macItem.BackColor = BtLabelModel.ColorModel.SecondColor;
                }
                macItems.Add(macItem);
            }
            Growl.Clear();
            if (models.Count == 0)
            {
                Growl.Warning("未查询到数据！");
            }
            else
            {
                Growl.Success("查询到" + models.Count + "条数据！");
            }
            viewModel.MacItems.Clear();
            viewModel.MacItems = macItems;
        }
    }
}
